package item;

import java.util.ArrayList;

import factory.EffectFactory;
import factory.InfluenceRadiusFactory;
import factory.ItemFactory;
import factory.SkillFactory;

import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveCollectionComposite;
import loadsave.loadsaveobject.LoadSaveIndex;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;
import occupation.OccupationType;
import stat.StatList;
import stat.NonEntityStatList;

public class Requirements implements loadsave.LoadSaveable {

	private ArrayList<OccupationType> occType;
	private ArrayList<String> reqdTriggers;
	private ArrayList<TakeableItem> reqdItems;
	private StatList minStats;
	
	public Requirements() {
		occType = new ArrayList<OccupationType>();
		reqdTriggers = new ArrayList<String>();
		reqdItems = new ArrayList<TakeableItem>();
		minStats = new NonEntityStatList();
	}

	public Requirements(ArrayList<OccupationType> occ, ArrayList<String> triggers, ArrayList<TakeableItem> items, StatList stats) {
		occType = occ;
		reqdTriggers = triggers;
		reqdItems = items;
		minStats = stats;
	}

	/**
	 * mutator for the list of occupations required
	 * @param occType
	 */
	public void setOccType(ArrayList<OccupationType> occType) {
		this.occType = occType;
	}

	/**
	 * accessor for the list of occupations required
	 * @return
	 */
	public ArrayList<OccupationType> getOccType() {
		return occType;
	}

	/**
	 * mutator for the triggers
	 * @param reqdTriggers
	 */
	public void setReqdTriggers(ArrayList<String> reqdTriggers) {
		this.reqdTriggers = reqdTriggers;
	}

	/**
	 * accessor for the triggers
	 * @return
	 */
	public ArrayList<String> getReqdTriggers() {
		return reqdTriggers;
	}

	/**
	 * mutator for the items required
	 * @param reqdItems
	 */
	public void setReqdItems(ArrayList<TakeableItem> reqdItems) {
		this.reqdItems = reqdItems;
	}
	
	/**
	 * accessor for the items required
	 * @return
	 */
	public ArrayList<TakeableItem> getReqdItems() {
		return reqdItems;
	}

	/**
     * mutator for the minimum stats required to
     * reach this requirement
     * @return
     */
	public void setMinStats(StatList minStats) {
		this.minStats = minStats;
	}

	/**
	 * accessor for the minimum stats required to
	 * reach this requirement
	 * @return
	 */
	public StatList getMinStats() {
		return minStats;
	}
	
	/**
	 * for saving and loading this class
	 */
    @Override
    public Memento getMemento() {
        return new Memento() {

            @Override
            public LoadSaveObject getState() {
                
                LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "Requirements");
                
                LoadSaveObject ms = minStats.getMemento().getState();
                ms.setName("minStats");
                toReturn.add(ms.getName(), ms);
                
                LoadSaveCollectionComposite list = new LoadSaveCollectionComposite("occType", "ArrayList", "String");
                int index = 0;
                for( OccupationType ot : occType) {
                    list.add(new LoadSaveIndex(index++), new LoadSavePrimitive("", "String", String.valueOf(ot)));
                }
                toReturn.add(list.getName(), list);
                
                list = new LoadSaveCollectionComposite("reqdTriggers", "ArrayList", "String");
                index = 0;
                for( String s : reqdTriggers) {
                    list.add(new LoadSaveIndex(index++), new LoadSavePrimitive("", "String", s));
                }
                toReturn.add(list.getName(), list);
                
                list = new LoadSaveCollectionComposite("reqdItems", "ArrayList", "TakeableItem");
                index = 0;
                for( TakeableItem ta : reqdItems) {
                    list.add(new LoadSaveIndex(index++), ta.getMemento().getState());
                }
                toReturn.add(list.getName(), list);
                
                // done - private ArrayList<OccupationType> occType;
                // done - private ArrayList<String> reqdTriggers;
                // done - private ArrayList<TakeableItem> reqdItems;
                
                
                return toReturn;
            }

            @Override
            public void restoreState(LoadSaveObject lso) {
                
                occType = new ArrayList<OccupationType>();
                Iterator<LoadSaveObject> i = lso.get("occType").getIterator();
                for (i.init(); i.hasNext(); i.next()) {
                    occType.add(OccupationType.valueOf(i.getCurrVal().getValue()));
                }
                
                reqdTriggers = new ArrayList<String>();
                i = lso.get("reqdTriggers").getIterator();
                for (i.init(); i.hasNext(); i.next()) {
                    reqdTriggers.add(i.getCurrVal().getValue());
                }
                
                reqdItems = new ArrayList<TakeableItem>();
                i = lso.get("reqdItems").getIterator();
                for (i.init(); i.hasNext(); i.next()) {
                    reqdItems.add(ItemFactory.createTakeableItem(i.getCurrVal()));
                }
                
                // done - private ArrayList<OccupationType> occType;
                // done - private ArrayList<String> reqdTriggers;
                // done - private ArrayList<TakeableItem> reqdItems;
                
                minStats = new NonEntityStatList();
                minStats.getMemento().restoreState(lso.get("minStats"));
           
            
            }
            
        };
    }
}
